<?php

namespace App\Controller\Api;

use App\Base\BaseController4Restful;
use App\Entity\UserAuth;
use App\Repository\UserAuthRepository;
use App\Service\ShopService;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ApiUserAuthController extends BaseController4Restful
{
    /**
     * @Route("/api/userAuths", name="apiGetUserAuths",methods={"GET","POST"})
     */
    public function apiGetUserAuths(UserAuthRepository $repository,Request $request)
    {
        switch ($request->getMethod()) {
            case "POST":
                $user = $this->needToBeUser();
                $data = $this->getRequestData();
                $data["code"] .= "";
                /**@var $UserAuth UserAuth*/
                if ($UserAuth = $user->getAuth()) {
                    if ($UserAuth->getStatus() == 1) {
                        return $this->json_out(400,"已通过认证无需重复认证");
                    }
                    $UserAuth = $this->updateEntity($data,UserAuth::class,$UserAuth);
                }else{
                    $UserAuth = $this->createEntity($data,UserAuth::class);
                }
                $UserAuth->setUser($user)
                    ->setCreateAt(new \DateTime())
                    ->setStatus(0);
                $this->myEm()->persist($UserAuth);$this->myEm()->flush();
                return $this->json_out(200,"创建成功", $UserAuth);
            default:
                if (!$page = $this->getParamRequired("page")) {$page = 1;}
                $limit = 10;
                $offset = (int)abs($limit * ($page - 1));
                $total = $repository->count([]);
                $this->setMeta($page,$total,$limit);
                $list = $repository->findBy(array(),array(),$limit,$offset);
                return $this->json_out(200,"获取成功", $list);
        }
    }

    /**
     * @Route("/api/userAuths/{id}", name="apiAddUserAuths",methods={"GET","PUT","PATCH","DELETE"})
     */
    public function apiUpdateUserAuths(UserAuth $UserAuth,Request $request,ShopService $shopService)
    {
        $data = $this->getRequestData();
        switch ($request->getMethod()) {
            case "DELETE":
                $this->myEm()->remove($UserAuth);$this->myEm()->flush();
                return $this->json_out(200,"已移除");
            case "PATCH":
            case "PUT":
                /**@var $UserAuth UserAuth*/
                $startStatus = $UserAuth->getStatus();
                $UserAuth = $this->updateEntity($data,UserAuth::class,$UserAuth);
                $endStatus = $UserAuth->getStatus();
                if ($request->getMethod() == "PATCH" && $startStatus == 0 && $endStatus == 1) {
                    //检查用户是否有相应标签，如果没有就增加
                    $shop = $UserAuth->getUser()->getShop();
                    if (!$shopService->hasTag($shop,ShopService::__TAG_PERSON__)) {
                        $shopService->addTagId($shop,ShopService::__TAG_PERSON__);
                    }
                }
                break;
            case "Get":
            default:
        }
        $this->myEm()->persist($UserAuth);$this->myEm()->flush();
        return $this->json_out(200,"获取成功", $UserAuth);
    }
}
